﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Interfleet.SE.UICompiler.Core;
using Interfleet.SE.UICompiler.Core.ClosureCompiler;
using Interfleet.SE.UICompiler.Tests.Stubs;

namespace Interfleet.SE.UICompiler.Tests
{
    /// <summary>
    /// Summary description for ArgumentBuilderTests
    /// </summary>
    [TestClass]
    public class ArgumentBuilderTests
    {
        public ArgumentBuilderTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private CCArguments GetArgs()
        {
            return new CCArguments { CompileOption = "WHITESPACE_ONLY" };
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentBuilder_Constructor_When_ArgumentsIsNull_Then_ArgumentNullExceptionIsThrown()
        {
            new CCArgumentBuilder(null);
            Assert.Fail();
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ArgumentBuilder_Constructor_When_FileSizeProviderIsNull_Then_ArgumentNullExceptionIsThrown()
        {
            new CCArgumentBuilder(GetArgs(), null);
            Assert.Fail();
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ArgumentBuilder_Constructor_When_InvalidCompileOption_ThenArgumentExceptionIsThrown()
        {
            new CCArgumentBuilder(new CCArguments { CompileOption = "Invalid" }, new FileInfoProviderStub());
            Assert.Fail();
        }

        [TestMethod]
        public void ArgumentBuilder_Build_VerifyArgumentStartsWithJar()
        {
            var args = GetArgs();
            args.CompiledFileName = "compiled-file.js";
            ArgumentBuilder builder = new CCArgumentBuilder(args, new FileInfoProviderStub());
            var result = builder.Build();
            Assert.IsTrue(result.StartsWith("-jar"), "Argument does not starts with -jar");
        }

        [TestMethod]
        public void ArgumentBuilder_Build_VerifyOutputFile()
        {
            var args = GetArgs();
            args.CompiledFileName = "compiled-file.js";
            ArgumentBuilder builder = new CCArgumentBuilder(args, new FileInfoProviderStub());
            var result = builder.Build();
            StringAssert.Contains(result, " --js_output_file \"compiled-file.js\"");
        }

        [TestMethod]
        public void ArgumentBuilder_Build_VerifyInputFile()
        {
            var args = GetArgs();
            args.CompiledFileName = "compiled-file.js";
            args.Files.Add("input-file.js");
            ArgumentBuilder builder = new CCArgumentBuilder(args, new FileInfoProviderStub());
            var result = builder.Build();
            StringAssert.Contains(result, " --js \"input-file.js\"");
        }

        [TestMethod]
        public void ArgumentBuilder_Build_VerifyTwoInputFiles()
        {
            var args = GetArgs();
            args.CompiledFileName = "compiled-file.js";
            args.Files.Add("input-file.js");
            args.Files.Add("input-file2.js");
            ArgumentBuilder builder = new CCArgumentBuilder(args, new FileInfoProviderStub());
            var result = builder.Build();
            StringAssert.Contains(result, " --js \"input-file.js\"", "Args did not contain file 1");
            StringAssert.Contains(result, " --js \"input-file2.js\"", "Args did not contain file 2");
        }

        [TestMethod]
        public void ArgumentBuilder_Build_VerifySummaryLevelDetail()
        {
            var args = GetArgs();
            ArgumentBuilder builder = new CCArgumentBuilder(args, new FileInfoProviderStub());
            var result = builder.Build();
            StringAssert.Contains(result, " --summary_detail_level 1");
        }

        [TestMethod]
        public void ArgumentBuilder_Build_VerifyCharSetIsUTF8()
        {
            var args = GetArgs();
            ArgumentBuilder builder = new CCArgumentBuilder(args, new FileInfoProviderStub());
            var result = builder.Build();
            StringAssert.Contains(result, " --charset UTF-8");
        }
    }
}
